﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using DotNet.Edu.Service;
using DotNet.Edu.WebUtility;
using DotNet.EduWebApi.Models;
using DotNet.Extensions;
using DotNet.Utility;

namespace DotNet.EduWebApi.Controllers
{
    public class SystemController : BaseApiController
    {
        /// <summary>
        /// 学员登录
        /// </summary>
        /// <param name="account">账号</param>
        /// <param name="password">密码</param>
        [ApiCheck,HttpPost]
        public ActionResult Login(string account, string password)
        {
            #region 验证
            if (account.IsEmpty())
            {
                return Json(new BoolMessage(false, "请指定账号"));
            }
            if (password.IsEmpty())
            {
                return Json(new BoolMessage(false, "请指定密码"));
            }
            #endregion

            #region 解密
            var validResult = EncryptUtil.Decrypt(account, "账号");
            if (!validResult.Success)
            {
                return Json(validResult);
            }
            account = validResult.Data;

            validResult = EncryptUtil.Decrypt(password, "密码");
            if (!validResult.Success)
            {
                return Json(validResult);
            }
            password = validResult.Data;
            #endregion

            var studentService = EduService.Student;
            var _result = studentService.Login(account, password);
            if (_result.Failure) //登陆失败
            {
                return Json(_result);
            }

            var entity = studentService.GetByIDCardNoOrMobile(account);
            studentService.UpdateExamStatus(entity);

            var audit = EduWebHelper.BuildStudentAuditEntity(entity);
            EduService.StudentAudits.Create(audit);

            var m = new LoginResultModel(true, string.Empty, entity.Id);
            m.Name = entity.Name;
            m.Category = entity.WorkCategoryName;
            m.Browser = audit.Browser;
            m.Device = audit.Device;
            m.OS = audit.OS;
            m.IPAddress = audit.IPAddress;
            m.LoginDateTime = audit.LoginDateTime;
            return Json(m);
        }

        /// <summary>
        /// 学员注销
        /// </summary>
        /// <param name="userId"></param>
        [ApiCheck, HttpPost]
        public ActionResult Logout(string userId)
        {
            #region 参数验证解密

            if (userId.IsEmpty())
            {
                return Json(new BoolMessage(false, "请指定学员主键"));
            }

            var validResult = EncryptUtil.Decrypt(userId, "学员主键");
            if (!validResult.Success)
            {
                return Json(validResult);
            }

            userId = validResult.Data;

            #endregion

            return Json(BoolMessage.True);
        }

        /// <summary>
        /// 检查版本更新
        /// </summary>
        [ApiCheck, HttpPost]
        public ActionResult CheckUpdate()
        {
            string path = Server.MapPath("~/version.json");
            var result = System.IO.File.ReadAllText(path);
            return new JsonStringResult(result);
        }


    }
}